﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using Ext.Net;
using BG.Data;
using BG.Service;
using Kwan.Helper.Bitmaps;
using Kwan.Data;

namespace BG.Web.Admin.Night
{
    public partial class Default : UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                //BtnAddClassInfo.Hidden = !permit[0];
                //AppendChild.Hidden = !permit[0];
                //BtnAddInfo.Hidden = !permit[0];
                //Rename.Hidden = !permit[2];
                ////btnChangeHot.Hidden = !permit[2];

                //BtnDeleteInfo.Hidden = !permit[3];
                //Remove.Hidden = !permit[3];
                //if (permit[3])
                //{
                //    List<GridCommand> gridCommand = new List<GridCommand>{
                //        ExtHelper.CreateGridCommand("Delete", Icon.Delete, "删除", "删除该条记录")
                //    };
                //    GridPanel1.ColumnModel.Columns.Add(ExtHelper.CreateCommandColumn(gridCommand));
                //}
                //ResourceManager1.RegisterIcon(Icon.Image);
                qStartDate.Text = DateTime.Now.AddDays(-1).ToShortDateString();
                qEndDate.Text = DateTime.Now.AddDays(-1).ToShortDateString();
            }
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] list = obj.Split(',');
            (TOrder.entityID == list).Delete<TOrder>();
            Store1.DataBind();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            IList<TOrder> list = JSON.Deserialize<IList<TOrder>>(obj);
            if (list.Count > 0)
            {
                CrudManage.ChangeState(list, NameValue);
                Store1.DataBind();
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void EditInfo()
        {
            if (!permit[2]) return;
            SetEditWindow();
            EditWindow.Show();
        }

        [DirectMethod]
        public void RowCommand(int orderID)
        {
            IList<VConsume> list = (TConsume.orderID == orderID).List<VConsume>();
            //for (int i = 0; i < id.Length; ++i)
            //{
            //    VProduct pro = proCrud.Service.ViewDAO.LoadById(id[i]);
            //    if (pro != null)
            //    {
            //        list.Add(pro);
            //    }
            //}
            ConsumeDS.DataSource = list;
            ConsumeDS.DataBind();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());

            //搜索项
            if (string.IsNullOrEmpty(txtFilter.Text))
                ps.Set("tag", string.Format(",{0},{1}", DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")));
            else
                ps.Set("tag", txtFilter.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = OrderQuery.MakeSearchWhere(ps);
            IList<VOrder> ListView = exp.List<VOrder>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TOrder>();
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void SetEditWindow()
        {
            NameValueCollection ps = new NameValueCollection();
            if (string.IsNullOrEmpty(txtFilter.Text))
                ps.Set("tag", string.Format(",{0},{1}", DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")));
            else
                ps.Set("tag", txtFilter.Text);
            //DateTime dt2 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 6:00:00");
            //DateTime dt1 = dt2.AddDays(-1);
            Expression exp1 = OrderQuery.MakeSearchWhere(ps);
            //exp &= TCollection.roomID != null;

            IList<VOrder> clist = exp1.List<VOrder>();

            double receivable = 0;
            double cash = 0;
            double pos = 0;
            double correctMoney = 0;
            foreach (VOrder o in clist)
            {
                if (o.Status == 2)
                {
                    cash += o.DPCashPay + o.CashPay + o.Change;
                    pos += o.DPPosPay + o.PosPay;
                }
                if (o.Status == 3)
                {
                    correctMoney += o.Receivable;
                }
                receivable += o.Receivable;
            }

            txtCorrect.Text = correctMoney.ToString();

            string[] vals = ps["tag"].Split(',');
            txtStartDate.Text = vals[1];
            txtEndDate.Text = vals[2];
            txtReceivable.Text = receivable.ToString();
            txtCash.Text = cash.ToString();
            txtPos.Text = pos.ToString();
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            //TOrder tt = Convert.ToInt32(txtID.Text) > 0 ? 
            //    (TOrder.entityID == Convert.ToInt32(txtID.Text)).ListFirst<TOrder>() : new TOrder();

            //tt.CardNO = txtCardNO.Text;
            //tt.CashPay = double.Parse(txtCashPay.Text);
            //tt.CheckInDate = DateTime.Now;
            //tt.CheckInPrice = double.Parse(txtCheckInPrice.Text);
            //tt.CheckInRoomPrice = double.Parse(txtCheckInRoomPrice.Text);
            //tt.CheckInType = int.Parse(txtCheckInType.Text);
            //tt.CustomerID = int.Parse(txtCustomerID.Text);
            //tt.Days = int.Parse(txtDays.Text);
            //tt.Deposit = double.Parse(txtDeposit.Text);
            //tt.DisCount = int.Parse(txtDisCount.Text);
            //tt.FinalRoomPrice = double.Parse(txtFinalRoomPrice.Text);
            //tt.Invoice = double.Parse(txtInvoice.Text);
            //tt.InvoiceNO = txtInvoiceNO.Text;
            //tt.Memo = txtMemo.Text;
            //tt.Pid = int.Parse(txtPid.Text);
            //tt.PosPay = double.Parse(txtPosPay.Text);
            //tt.RoomID = int.Parse(txtRoomID.Text);
            //tt.Status = int.Parse(txtStatus.Text);
            //tt.TotalPrice = double.Parse(txtTotalPrice.Text);
            //tt.Enable = txtEnable.Checked;
            //tt.Hot = txtHot.Checked;

            //DBContext.Save(tt);
            //Store1.DataBind();
            //Store1.Dispose();
            //EditWindow.Hide();
        }
    }
}